<?php

//运单控制器
class WaybillApp extends MallbaseApp
{
var $_waybill_mod;
	var $_dictionary_mod;
	var $_user_mod;
	var $canedit;
	var $_haul_mod;
	var $_exception_mod;
	var $_exattr_mod;

	function __construct()
	{
		$this->WaybillApp();
	}

	function WaybillApp()
	{
		parent::__construct();

		$this->_waybill_mod =& m('waybill');
		$this->_dictionary_mod =& m('dictionary');
		$this->_user_mod =& m('member');
		$this->_haul_mod =& m('haul');
		$this->_exception_mod =& m('exception');
		$this->_exattr_mod =& m('exattr');
		$this->canedit=false;
	}

	function index()
	{
		$db=&db();
		$types=array();
		$types['集港'] = '集港';
		$types['疏港'] = '疏港';
		$types['调空箱'] = '调空箱';
		$types['仓库空箱'] = '仓库空箱';

		$this->assign('types', $types);

		$sql="SELECT d_value FROM zys_dictionary WHERE d_code='wstatus'";
		$wstatus=$db->getAll($sql);
		$status=array();
		foreach ($wstatus as  $v){
			$status[$v['d_value']]=$v['d_value'];
		}
		$status[''] = '全部';
		$this->assign('status', $status);

		$type_id=empty($_GET['type_id'])?'':$_GET['type_id'];
		$mstatus=empty($_GET['status'])?'':$_GET['status'];
		$wm_driver=empty($_GET['wm_driver'])?'':$_GET['wm_driver'];
		$wm_vhc_code=empty($_GET['wm_vhc_code'])?'':$_GET['wm_vhc_code'];
		$wm_code=empty($_GET['wm_code'])?'':$_GET['wm_code'];

		$conditions = "1=1 ";
		if($wm_code){
			$conditions.=" and wm_code LIKE '%".$wm_code."%'";
		}
		if($wm_driver){
			$conditions.=" and (wm_driver LIKE '%".$wm_driver."%' OR wm_driver_tel LIKE '%".$wm_driver."%' )";
		}
		if($wm_vhc_code){
			$conditions.=" and wm_vhc_code LIKE '%".$wm_vhc_code."%' ";
		}

		if($type_id){
			$conditions.="  AND wm_type ='".$type_id."'";
		}

		if($mstatus!=null&&$mstatus!=''){
			$conditions.=" and wm_stauts='".$mstatus."' ";
		}
		/*$user_ids = $this->checkpower();
		 if(!empty($user_ids)){
			$conditions.=" and wm_optid in(".$user_ids.")";
			}*/
		//制单时间
		if($_GET['add_time_from']!=''){
			$add_time_from = gmstr2time($_GET['add_time_from']);
			$conditions.=" and wm_opdate >= $add_time_from";
		}
		if($_GET['add_time_to']!=''){
			$add_time_to = gmstr2time_end($_GET['add_time_to']);
			$conditions.=" and wm_opdate <= $add_time_to";
		}
		$page   =   $this->_get_page(10);   //获取分页信息

		$sql2 = "SELECT distinct waybill_main.*
FROM zys_waybill_main waybill_main 
				WHERE  ".$conditions." ORDER BY wm_tid desc LIMIT ".$page['limit'];
		$waybill = $db->getAll($sql2);

		$sql3=" SELECT COUNT(*) FROM (SELECT DISTINCT  waybill_main.*
FROM zys_waybill_main waybill_main 
where $conditions
GROUP BY waybill_main.wm_tid  ) AA   ";

		$page['item_count']=$db->getOne($sql3);   //获取统计数据

		$this->_format_page($page);

		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('waybill', $waybill);
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

		$this->assign('tabself', json_encode(array('text'=>'运单管理')));
		$this->display('waybill.index.html');
	}

function view(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST){
			$waybill=$this->_waybill_mod->get(array(
	        'fields'   => 'this.*,member.user_name,member.real_name',
	        'conditions'  => 'wm_tid='.$id,
			'join'=>'belongs_to_user',
			));
			$db=&db();
			$normal=true;
			if($waybill['wm_type']=='集卡进口'||$waybill['wm_type']=='集卡出口'){
				$normal=false;
				$sql="SELECT * from zys_waybill_sub_trunk w LEFT JOIN zys_goods_trunk_sub g on w.ws_gstid=g.gs_tid LEFT JOIN zys_goods_trunk t on t.g_tid=g.gs_gtid where ws_wmtid=".$id;
				$sub=$db->getAll($sql);
				$sublist=array();
				if($sub){
					foreach ($sub as $v){
						$sublist[$v['gs_gtid']]['g_tid']=$v['g_tid'];
						$sublist[$v['gs_gtid']]['g_companytid']=$v['g_companytid'];
						$sublist[$v['gs_gtid']]['g_ship']=$v['g_ship'];
						$sublist[$v['gs_gtid']]['g_voyage']=$v['g_voyage'];
						$sublist[$v['gs_gtid']]['g_awb_code']=$v['g_awb_code'];
						$sublist[$v['gs_gtid']]['g_packingtime']=$v['g_packingtime'];
						$sublist[$v['gs_gtid']]['g_collectiontime']=$v['g_collectiontime'];
						$sublist[$v['gs_gtid']]['g_container_depot']=$v['g_container_depot'];
						$sublist[$v['gs_gtid']]['g_container_lng']=$v['g_container_lng'];
						$sublist[$v['gs_gtid']]['g_container_lat']=$v['g_container_lat'];
						$sublist[$v['gs_gtid']]['g_container_man']=$v['g_container_man'];
						$sublist[$v['gs_gtid']]['g_container_tel']=$v['g_container_tel'];
						$sublist[$v['gs_gtid']]['g_full_container_depot']=$v['g_full_container_depot'];
						$sublist[$v['gs_gtid']]['g_full_container_lng']=$v['g_full_container_lng'];
						$sublist[$v['gs_gtid']]['g_full_container_lat']=$v['g_full_container_lat'];
						$sublist[$v['gs_gtid']]['g_full_container_man']=$v['g_full_container_man'];
						$sublist[$v['gs_gtid']]['g_full_container_tel']=$v['g_full_container_tel'];
						$sublist[$v['gs_gtid']]['g_loading_place']=$v['g_loading_place'];
						$sublist[$v['gs_gtid']]['g_loading_lng']=$v['g_loading_lng'];
						$sublist[$v['gs_gtid']]['g_loading_lat']=$v['g_loading_lat'];
						$sublist[$v['gs_gtid']]['g_loading_man']=$v['g_loading_man'];
						$sublist[$v['gs_gtid']]['g_loading_tel']=$v['g_loading_tel'];
						$sublist[$v['gs_gtid']]['g_note']=$v['g_note'];
						$sublist[$v['gs_gtid']]['g_tgdate']=$v['g_tgdate'];
						$sublist[$v['gs_gtid']]['box'][]=$v;
					}
				}
			}else{
				$sql="SELECT * from zys_waybill_sub_normal w LEFT JOIN zys_goods_normal_sub g on w.ws_gstid=g.gs_tid LEFT JOIN zys_goods_normal t on t.g_tid=g.gs_gtid where ws_wmtid=".$id;
				$sub=$db->getAll($sql);
				$sublist=array();
				if($sub){
					foreach ($sub as $v){
						$sublist[$v['gs_gtid']]['g_tid']=$v['g_tid'];
						$sublist[$v['gs_gtid']]['g_ownertid']=$v['g_ownertid'];
						$sublist[$v['gs_gtid']]['g_owner']=$v['g_owner'];
						$sublist[$v['gs_gtid']]['g_owner_tel']=$v['g_owner_tel'];
						$sublist[$v['gs_gtid']]['g_loading_place']=$v['g_loading_place'];
						$sublist[$v['gs_gtid']]['g_loading_lng']=$v['g_loading_lng'];
						$sublist[$v['gs_gtid']]['g_loading_lat']=$v['g_loading_lat'];
						$sublist[$v['gs_gtid']]['g_unloading_place']=$v['g_unloading_place'];
						$sublist[$v['gs_gtid']]['g_loading_date']=$v['g_loading_date'];
						$sublist[$v['gs_gtid']]['g_unloading_date']=$v['g_unloading_date'];
						$sublist[$v['gs_gtid']]['g_unloading_lng']=$v['g_unloading_lng'];
						$sublist[$v['gs_gtid']]['g_unloading_lat']=$v['g_unloading_lat'];
						$sublist[$v['gs_gtid']]['g_consignee']=$v['g_consignee'];
						$sublist[$v['gs_gtid']]['g_consignee_tel']=$v['g_consignee_tel'];
						$sublist[$v['gs_gtid']]['g_gtype']=$v['g_gtype'];
						$sublist[$v['gs_gtid']]['g_consignor']=$v['g_consignor'];
						$sublist[$v['gs_gtid']]['g_consignor_tel']=$v['g_consignor_tel'];
						$sublist[$v['gs_gtid']]['g_note']=$v['g_note'];
						$sublist[$v['gs_gtid']]['box'][]=$v;
					}
				}
			}

			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);

			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('ctype', $typelist);
			$sql="SELECT * from zys_haul where h_wmtid=".$id." ORDER BY h_order";
			$haul=$db->getAll($sql);

			$haullist=array();
			if($haul){
				foreach ($haul as $v){
					$sql="SELECT * from zys_haul_collect WHERE hc_h_tid=".$v['h_tid'];
					$collect=$db->getAll($sql);
					if($collect){
						foreach ($collect as $vv){
							$arrs=array();
							if($vv['h_collect_val']){
								$vv['h_collect_val']=trim($vv['h_collect_val'],',');
								$arrs=explode(',', $vv['h_collect_val']);
							}
							$vv['ar']=$arrs;
							$v['item'][]=$vv;
						}
					}

					$haullist[]=$v;
				}
			}
			//异常信息
			$exception=array();
			$sql="SELECT * FROM zys_exception WHERE e_w_tid=$id";
			$exception=$db->getAll($sql);
			if($exception){
				foreach ($exception as $k=>$v){
					$sql="SELECT * FROM zys_exception_attr WHERE ea_type=0 and ea_e_tid=".$v['e_tid'];
					$exception[$k]['e_pic']=$db->getAll($sql);

					$sql="SELECT * FROM zys_exception_attr WHERE ea_type!=0 and ea_e_tid=".$v['e_tid'];
					$exception[$k]['e_voice']=$db->getAll($sql);
				}
			}
			$this->assign('exception', $exception);
			$this->assign('haullist', $haullist);
			$this->assign('normal', $normal);
			$this->assign('waybill', $waybill);
			$this->assign('sublist', $sublist);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
				'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->display('waybill.view.html');
		}
	}
}